﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Ibp.Service.Entity.ODAEntity;
using OdaOperation.Core.Parameters;
using OdaOperation.Extends;
using Teigha.Core;
using Teigha.TD;

namespace OdaOperation.Core.Creators
{
    public class BlockTableRecordCreator : BaseCreator, IReferenceCreator
    {
        private string m_blockName;

        public BlockTableRecordCreator(OdDbDatabase pDb)
            : base(pDb)
        {
        }

        public BlockTableRecordCreator(OdDbDatabase pDb, string blockName)
            : base(pDb)
        {
            m_blockName = blockName;
        }

        public static OdDbBlockTableRecord CreateOdDbBlockTableRecord(OdDbDatabase database, string name)
        {
            OdDbBlockTable odDbBlockTable = database.getBlockTableId().openObject(OpenMode.kForWrite) as OdDbBlockTable;
            OdDbBlockTableRecord odDbBlockTableRecord = OdDbBlockTableRecord.createObject();
            odDbBlockTableRecord.setName(name);
            odDbBlockTable.upgradeOpen();
            odDbBlockTable.add(odDbBlockTableRecord);
            odDbBlockTable.downgradeOpen();
            return odDbBlockTableRecord;
        }

        public void LoadFromFile(string fileName, string blockName)
        {
            m_blockName = blockName;
            MemoryTransaction value = MemoryManager.GetMemoryManager().StartTransaction(Thread.CurrentThread.ManagedThreadId);
            try
            {
                if (!File.Exists(fileName))
                {
                    throw new Exception("块文件" + fileName + "文件不存在！");
                }

                OdDbBlockTable odDbBlockTable = (OdDbBlockTable)m_database.getBlockTableId().safeOpenObject(OpenMode.kForWrite);
                if (!odDbBlockTable.has(blockName))
                {
                    ExHostAppServices exHostAppServices = new ExHostAppServices();
                    OdDbDatabase pSource = exHostAppServices.readFile(fileName);
                    OdDbObjectId odDbObjectId = m_database.insert(blockName, pSource);
                    return;
                }

                throw new Exception("块名称已经存在！");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                MemoryManager.GetMemoryManager().StopTransaction(value);
            }
        }

        public Dictionary<string, Type> GetParameter()
        {
            MemoryTransaction value = MemoryManager.GetMemoryManager().StartTransaction(Thread.CurrentThread.ManagedThreadId);
            try
            {
                m_blockTableId = (m_database.getBlockTableId().safeOpenObject(OpenMode.kForRead) as OdDbBlockTable).getAt(m_blockName);
                OdDbBlockTableRecord odDbBlockTableRecord = m_blockTableId.openObject(OpenMode.kForRead) as OdDbBlockTableRecord;
                OdDbObject odDbObject = odDbBlockTableRecord.getFieldDictionary().openObject(OpenMode.kForRead);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            MemoryManager.GetMemoryManager().StopTransaction(value);
            return new Dictionary<string, Type>();
        }

        public void CreateBlockReference(string layerName, DPoint location, OdGeScale3d odGeScale = null, double rotating = 0.0, OdGePlane mirrorPlane = null, ParameterSet paramters = null)
        {
            new BlockReferenceCreator(m_database, m_blockName, layerName).Create(location.ToGePoint3D(), odGeScale, rotating, mirrorPlane, paramters);
        }

        public void CreateBlockReference(ReferenceParameter paramter)
        {
            new BlockReferenceCreator(m_database, paramter.BlockName, paramter.LayerName).Create(paramter.Location.ToGePoint3D(), paramter.Scale, paramter.Rotating, paramter.MirrorPlane, paramter.Parameters);
        }

        public BlockReferenceCreator GetBlockReferenceCreator(string layerName)
        {
            return new BlockReferenceCreator(m_database, m_blockName, layerName);
        }
    }
}